package com.fzhucc.modules.lesson.service.impl;

import com.fzhucc.exception.BadRequestException;
import com.fzhucc.modules.lesson.domain.Lesson;
import com.fzhucc.modules.lesson.domain.LessonLabel;
import com.fzhucc.modules.lesson.domain.dto.LessonDoLabelDto;
import com.fzhucc.modules.lesson.domain.dto.LessonLabelDto;
import com.fzhucc.modules.lesson.domain.dto.LessonLabelQueryCriteria;
import com.fzhucc.modules.lesson.repository.LessonLabelRepository;
import com.fzhucc.modules.lesson.repository.LessonRepository;
import com.fzhucc.modules.lesson.service.LessonLabelService;
import com.fzhucc.modules.lesson.service.mapstruct.LessonLabelMapper;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import com.fzhucc.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
* @description 服务实现
* @author yyg
* @date 2022-12-07
**/
@Service
@RequiredArgsConstructor
public class LessonLabelServiceImpl implements LessonLabelService {

    private final LessonLabelRepository lessonLabelRepository;
    private final LessonLabelMapper lessonLabelMapper;

    private final LessonRepository lessonRepository;

    @Override
    public Map<String,Object> queryAll(LessonLabelQueryCriteria criteria, Pageable pageable){
        Page<LessonLabel> page = lessonLabelRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(lessonLabelMapper::toDto));
    }

    @Override
    public List<LessonLabelDto> queryAll(LessonLabelQueryCriteria criteria){
        return lessonLabelMapper.toDto(lessonLabelRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public LessonLabelDto findById(Integer id) {
        LessonLabel lessonLabel = lessonLabelRepository.findById(id).orElseGet(LessonLabel::new);
        ValidationUtil.isNull(lessonLabel.getId(),"LessonLabel","id",id);
        return lessonLabelMapper.toDto(lessonLabel);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LessonLabelDto create(LessonLabel resources) {
        return lessonLabelMapper.toDto(lessonLabelRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(LessonLabel resources) {
        LessonLabel lessonLabel = lessonLabelRepository.findById(resources.getId()).orElseGet(LessonLabel::new);
        ValidationUtil.isNull( lessonLabel.getId(),"LessonLabel","id",resources.getId());
        lessonLabel.copy(resources);
        lessonLabelRepository.save(lessonLabel);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            lessonLabelRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<LessonLabelDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (LessonLabelDto lessonLabel : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("课程id", lessonLabel.getLessonId());
            map.put("课程标签", lessonLabel.getLabelName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    @Transactional
    public void doLessonLabel(LessonDoLabelDto lessonDoLabelDto) {
        if (lessonDoLabelDto.getLabels() == null){
            throw new BadRequestException("课程标签未找到");
        }
        Optional<Lesson> optional = lessonRepository.findById(lessonDoLabelDto.getLesson_id());
        if (!optional.isPresent()){
            throw new BadRequestException("课程未找到");
        }
        //先删除课程原有标签  在绑定新的标签
        lessonLabelRepository.deleteByLessonId(lessonDoLabelDto.getLesson_id());
        for (String label : lessonDoLabelDto.getLabels()) {
            LessonLabel lessonLabel = new LessonLabel();
            lessonLabel.setLessonId(lessonDoLabelDto.getLesson_id());
            lessonLabel.setLabelName(label);
            lessonLabelRepository.save(lessonLabel);
        }
    }
}